package com.idis.managerApp.controller;

import com.idis.common.base.BaseController;
import com.idis.common.util.SecurityUtil;
import com.idis.entity.User;
import com.idis.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Ming on 2018/1/28.
 */
@RestController
@RequestMapping("/api/admin/user")
@Api(tags = "登录用户接口")
public class UserController extends BaseController{

    @Autowired
    UserService userService;

    @ApiOperation("获取所有用户信息")
    @GetMapping("/all")
    public Map showAll(){
        return sendDate(userService.findAll());
    }

    @ApiOperation("新增用户")
    @PostMapping("/save")
    public Map saveUser(@ApiParam("用户信息")User user){
        Map map=new HashMap();
        if(userService.finndByName(user.getUsername())!=null){
            addResult(map, "0");
            addMessage(map,"用户名已存在.");
            return map;
        }
        if(user.getPassword()==null||user.getPassword().equals("")||user.getAuthorities().equals("")||user.getUsername().equals("")){
            sendNull(map);
            return map;
        }
        user.setPassword(SecurityUtil.encode(user.getPassword()));
        addResult(map, String.valueOf(userService.savaUser(user)));
        addMessage(map,"保存成功");
        return map;
    }

    @ApiOperation("更新用户的信息")
    @PostMapping("/update")
    public Map update(@ApiParam("用户信息，若某信息不需要更新则可置空，主要用于更改其他信息时不对密码进行修改，,ID必须")User user){
        Map map=new HashMap();
        if(userService.finndByName(user.getUsername())!=null&&!userService.finndByName(user.getUsername()).get(0).getId().equals(user.getId())){
            addResult(map, "0");
            addMessage(map,"用户名已存在.");
            return map;
        }
        if(user.getPassword()!=null&&!user.getPassword().equals(""))
            user.setPassword(SecurityUtil.encode(user.getPassword()));
        if(user.getId()==null||user.getUsername()==null||user.getId().equals("")||user.getRole()==null||user.getUsername().equals("")){
            sendNull(map);
            return map;
        }
        addResult(map, String.valueOf(userService.updateNotNull(user)));
        addMessage(map,"修改成功");
        return map;
    }

    @ApiOperation("根据属性查询用户")
    @RequestMapping("/findbyproperty")
    public Map findByProp(@ApiParam("需要进行查询的用户属性，若某信息无需用于查询，则可置空")User user){
        return sendDate(userService.findByProperty(user));
    }

//    @GetMapping("/findbyproperty")
    public Class<? extends List> findByProp1(User user){
        return userService.findByProperty(user).getClass();
    }

    @ApiOperation("获取用户权限类型及代号")
    @GetMapping("/roles")
    public Map getRole(){
        return sendDate(userService.getRole());
    }

    @ApiOperation("根据用户ID查找用户")
    @GetMapping("/id/{id}")
    public Map finById(@ApiParam("用户ID")@PathVariable String id){
        return sendDate(userService.findByKey(id));
    }

    @ApiOperation("根据用户名查找用户")
    @GetMapping("/name/{username}")
    public Map finByName(@ApiParam("用户名")@PathVariable String username){
        return sendDate(userService.finndByName(username));
    }

    @ApiOperation("删除用户")
    @PostMapping("/delete")
    public Map delete(@ApiParam("用户信息，需要且仅需要用户ID")User user){
        Map map=new HashMap();
        addResult(map, String.valueOf(userService.delete(user)));
        addMessage(map,"删除成功");
        return map;
    }
}
